package nl.alwinzwets.controllers
{
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.net.URLRequest;
	
	import nl.alwinzwets.events.GVEvents;
	import nl.alwinzwets.visuals.Blad;
	import nl.alwinzwets.visuals.Bloem;
	import nl.alwinzwets.visuals.Stengel;
	
	public class Plant extends MovieClip
	{
		
		private var _stengel:Stengel;
		public var _bloem:Bloem;
		public var _bloemFocus:Bloem;
		private var _blad:Blad;
		private var _bloemX:int;
		private var _bloemY:int;
		
		private var _loader:Loader;
		private var _url:URLRequest;
		
		private var _stngl:int;
		private var _bld:int;
		private var _blm:int;
		private var _mldrd:int;
		private var _stmpr:int;
		
		public var bloemCenterX:int;
		public var bloemCenterY:int;
		public var bloemCenterR:int;
		
		public var bladCenterX:int;
		public var bladCenterY:int;
		public var bladCenterR:int;
		
		
		public function Plant(loader:Loader,stngl:int,bld:int,blm:int,mldrd:int,stmpr:int)
		{
			_loader = loader;
			_stngl = stngl;
			_bld = bld;
			_blm = blm;
			_mldrd = mldrd;
			_stmpr = stmpr;
			
			addStengel();
			
		}
		
		//** Loader handling
		//--------------------------------------------------------------------------------
		
		private function loaderHandler(e:Event):void
		{
			addStengel();
		}
		
		private function getClass(className:String):Class {
        	try {
            	return _loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
        	} catch (e:Error) {
				
        	}
    	    return null;
	    }
	    
	    private function getHook(mc:MovieClip, nr:int, name:String):MovieClip
	    {
	    	
        	try {
        		mc[name+nr].alpha = 0;
            	return mc[name+nr];
        	} catch (e:Error) {
				
        	}
    	    return null;
	    }

		
		//** Stengel controller
		//--------------------------------------------------------------------------------
		
		
		private function addStengel():void
		{
			// Get random Stengel MC
			var n:String = "Stengel"+_stngl;
    		var loadedStengel:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(n);
    		var mcStengel:MovieClip = new loadedStengel as MovieClip;
			
			_stengel = new Stengel(mcStengel);
			addChild(_stengel);
			
			
			var b:int = 1;
			var bladNr:String = "Blad"+_bld;
			
			while(getHook(_stengel.mc, b, "hookb") != null){ 
				var hookB:MovieClip =  getHook(_stengel.mc, b, "hookb");
				bladCenterX = hookB.x;
				bladCenterY = hookB.y;
				bladCenterR = hookB.rotation;
				addBlad(hookB.x, hookB.y, hookB.rotation, bladNr);
				b++;
			} 
			
			var i:int = 1;
			var bloemNr:String = "Bloem"+_blm;
			
			while(getHook(_stengel.mc, i, "hook") != null){ 
				var hook:MovieClip =  getHook(_stengel.mc, i, "hook");
				bloemCenterX = hook.x;
				bloemCenterY = hook.y;
				bloemCenterR = hook.rotation;
				addBloem(hook.x, hook.y, hook.rotation, bloemNr);
				i++;
			} 
			
			dispatchEvent(new Event(GVEvents.PLANT_COMPLETE));
		}
		
		
		//** Blad controller
		//--------------------------------------------------------------------------------
				
		private function addBlad(bX:int, bY:int, bR:Number, bN:String):void
		{	
			// Get random Bloem MC
    		var loadedBlad:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(bN);
    		var mcBlad:MovieClip = new loadedBlad as MovieClip;
			
			var tempBlad:Blad = new Blad(mcBlad);
			addChild(tempBlad);
			_blad = tempBlad;
			tempBlad.x = bX; 
			tempBlad.y = bY;
			tempBlad.rotation = bR;
			
		}
		
		//** Bloem controller
		//--------------------------------------------------------------------------------
				
		private function addBloem(bX:int, bY:int, bR:Number, bN:String):void
		{
			
			// Get random Bloem MC
    		var loadedBloem:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(bN);
    		var mcBloem:MovieClip = new loadedBloem as MovieClip;
    		
    		// Assign meeldraad
    		var meelNr:String = "Meeldraad"+_mldrd+"_front";
			var loadedMeel:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(meelNr);
    		var mcMeel:MovieClip = new loadedMeel as MovieClip;
    		
    		// Assign meeldraad
    		var meelNrA:String = "Meeldraad"+_mldrd+"_back";
			var loadedMeelA:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(meelNrA);
    		var mcMeelA:MovieClip = new loadedMeelA as MovieClip;
    		
    		// Assign meeldraad
    		var stampNr:String = "Stamper"+_stmpr;
			var loadedStamp:Object = _loader.contentLoaderInfo.applicationDomain.getDefinition(stampNr);
    		var mcStamp:MovieClip = new loadedStamp as MovieClip;
			
			
				_bloem = new Bloem(mcBloem, mcMeel, mcMeelA, mcStamp);
				addChild(_bloem);
				
				_bloem.x = bX; 
				_bloem.y = bY;
				_bloem.rotation = bR;
		
		}

	}
}